//
//  LevelGageItemController.m
//  XTCloud
//
//  Created by QA on 2018/7/24.
//  Copyright © 2018年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "LevelGageItemController.h"
#import "LevelGageItemCell.h"

#import "UITableView+Custom.h"
#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"
#import "WebServiceCommonRequest.h"

#import "MJRefresh.h"

@interface LevelGageItemController ()

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *resultData;

@property (strong, nonatomic) NSDictionary *lineDictionary;

@end

@implementation LevelGageItemController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.lineDictionary = self.value;
    self.resultData = [NSMutableArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] init];
    [self setupRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.1170927214197
}


- (void)setupRefresh
{
    //添加下拉加载数据
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

#pragma mark 开始进入刷新状态

- (void)headerRereshing
{
    [self getNetWorkExternalData];
}

#pragma mark 网络获取数据

- (void)getNetWorkExternalData
{
    NSString *scxID = [self.lineDictionary objectFormatStringForKey:@"ID"];
    
    //@审核状态（0未审核，1已审核, 2已拒绝，4待接单）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"ZNYJ-LWJTC-SCX-MX"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    [content addParameterKey:@"生产线ID" value:scxID];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [self.tableView headerEndRefreshing];
        
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>=1){
            firstArray = [result.resultArray firstObject];
        }
        
        //显示数据处理
        [self.resultData removeAllObjects];
        if(firstArray && firstArray.count>0){
            [self.resultData addObjectsFromArray:firstArray];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
            
        } else {
            [self.tableView setTableViewNotDataDrawText:@"暂无数据" isShow:YES];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"无法连接网络"];
    }];
    
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.resultData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifierOne = @"LevelGageItemCell";
    LevelGageItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierOne];
    if (!cell) {
        cell = [[LevelGageItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierOne];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *mDictionary = [self.resultData objectAtIndex:indexPath.section];
    cell.clLabel.text = [mDictionary objectFormatStringForKey:@"材料名称"];
    cell.clggLabel.text = [mDictionary objectFormatStringForKey:@"材料规格"];
    
    cell.krLabel.text = [mDictionary objectFormatFloatStringForKey:@"空容" retain:1];
    cell.xcLabel.text = [mDictionary objectFormatFloatStringForKey:@"现存" retain:1];
    cell.skLabel.text = [mDictionary objectFormatFloatStringForKey:@"上空" retain:1];
    cell.lgLabel.text = [mDictionary objectFormatFloatStringForKey:@"料高" retain:1];
    
    NSInteger jlState = [mDictionary objectFormatIntegerForKey:@"进料状态"];
    NSInteger ccState = [mDictionary objectFormatIntegerForKey:@"除尘状态状态"];
    NSInteger hlState = [mDictionary objectFormatIntegerForKey:@"耗料状态"];
    NSInteger bjState = [mDictionary objectFormatIntegerForKey:@"报警状态"];
    
    CGFloat xc = [mDictionary objectFormatFloatForKey:@"现存" retain:1 except:1];
    CGFloat zr = [mDictionary objectFormatFloatForKey:@"总容" retain:1 except:1];
    cell.waveProgress.progress = (zr<=0) ? 0 : xc/zr;
    cell.waveProgress.textLabelSub.text = [NSString stringWithFormat:@"总容%0.1f吨",zr];
    
    //进料状态
    if(jlState == 1) {
        cell.jlztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#16A87A"];
        cell.jlztLabel.textColor = [UIColorDispose colorWithHexString:@"#16A87A"];
        cell.jlztLabel.text = @"进料中";
    } else {
        cell.jlztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#AAAAAA"];
        cell.jlztLabel.textColor = [UIColor darkGrayColor];
        cell.jlztLabel.text = @"待料中";
    }
    
    //除尘状态
    if(ccState == 1) {
        cell.ccztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#16A87A"];
        cell.ccztLabel.textColor = [UIColorDispose colorWithHexString:@"#16A87A"];
        cell.ccztLabel.text = @"除尘中";
    } else {
        cell.ccztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#AAAAAA"];
        cell.ccztLabel.textColor = [UIColor darkGrayColor];
        cell.ccztLabel.text = @"除尘空闲";
    }
    
    //耗料状态
    if(hlState == 1) {
        cell.hlztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#16A87A"];
        cell.hlztLabel.textColor = [UIColorDispose colorWithHexString:@"#16A87A"];
        cell.hlztLabel.text = @"耗料中";
    } else {
        cell.hlztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#AAAAAA"];
        cell.hlztLabel.textColor = [UIColor darkGrayColor];
        cell.hlztLabel.text = @"耗料空闲";
    }
    
    //报警状态
    if(bjState == 1) {
        cell.bjztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#FF6347"];
        cell.bjztLabel.textColor = [UIColorDispose colorWithHexString:@"#FF6347"];
        cell.bjztLabel.text = @"报警中";
        cell.waveProgress.backWaveColor = [UIColorDispose colorWithHexString:@"#FA8072"];
        cell.waveProgress.frontWaveColor = [UIColorDispose colorWithHexString:@"#FF6347"];
        
    } else {
        cell.bjztRadius.backgroundColor = [UIColorDispose colorWithHexString:@"#AAAAAA"];
        cell.bjztLabel.textColor = [UIColor darkGrayColor];
        cell.bjztLabel.text = @"未报警";
        cell.waveProgress.backWaveColor = [UIColor colorWithRed:136/255.0f green:199/255.0f blue:190/255.0f alpha:1];// #88C7BE
        cell.waveProgress.frontWaveColor = [UIColor colorWithRed:28/255.0 green:203/255.0 blue:174/255.0 alpha:1]; // #1CCBAE
    }
    
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0) return 10;
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 155;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
